#!/usr/bin/env bash

pwdd_file="$HOME/git/run/etc/pwdd_file.data"

out_file()
{
    cat $pwdd_file| sort -t '=' -k 2 | awk -F'=' '{ printf("%-22s=%s\n", $1,$2)}'|\
        awk -F'pwdd_tmp_dir_' '{ printf(". cdd %s\n", $2)}'
    #return 0;
    #cat $pwdd_file | awk -F'=' '{ printf("%-22s=%s\n", $1,$2)}'|\
    #    awk -F'pwdd_tmp_dir_' '{ printf(". cdd %s\n", $2)}'
}



do_cdd()
{
    source "$pwdd_file"

    if [ -z "$1" ] || [ x"$1" == x"l" ]; then
        #echo "ll:$1"
        out_file $dir_name
    else
        #echo $pwdd_tmp_dir
        grep -w "pwdd_tmp_dir_$1" $pwdd_file > /dev/null
        if [ $? -ne 0 ];then
            #cat $pwdd_file
            out_file $dir_name
            echo
            echo -- warn,\"$1\" not exist, please use pwdd set it firstly
        else
            eval "echo \$pwdd_tmp_dir_$1"
            eval "cd \$pwdd_tmp_dir_$1"
            ls
        fi
    fi

}

dir_name=$1
main()
{
    if [ ! -f "$pwdd_file" ] ; then
        echo "-- error, cfg file is not exist, use pwdd firstly, filename:$pwdd_file"
    else
        do_cdd $dir_name $dir_name
    fi
}
main 

